﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EquipmentConfig.Class;
using System.Threading;

namespace EquipmentConfig
{
    public partial class ConnectionForm : Form
    {
        public ConnectionForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 从注册表获取系统串口列表
        /// </summary>
        public void InitComList()
        {
            try
            {
                RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
                string[] sSubKeys = keyCom.GetValueNames();
                for (int i = 0; i < sSubKeys.Length; i++)
                {
                    string strPortName = (string)keyCom.GetValue(sSubKeys[i]);
                    cbbSerialPort.Properties.Items.Add(strPortName);
                }

                if (cbbSerialPort.Properties.Items.Count > 0)
                {
                    cbbSerialPort.SelectedIndex = 0;
                }
            }
            catch
            {
                Messages.ShowAlertDialog("加载串口失败！", false);
            }
        }

        private void ConnectionForm_Load(object sender, EventArgs e)
        {
            InitComList();
        }

        /// <summary>
        /// 串口连接
        /// </summary>
        private void btnCk_Click(object sender, EventArgs e)
        {
            if (cbbSerialPort.Properties.Items.Count == 0)
            {
                Messages.ShowAlertDialog("无可用串口!",false);
                return;
            }

            MainForm mf = new MainForm();
            if (mf.OpenSerialPort(cbbSerialPort.EditValue.ToString()))
            {
                MainForm.isReadDeviceSuccess = false;
                //mf.SendDeviceCmd();
                if (mf.CheckReturnCmd1())
                {
                    mf.Show();
                    this.Hide();
                }
                else
                {
                    mf.CloseConnection(0);
                    Messages.ShowAlertDialog("连接设备失败，请重新连接！", false);
                }
            }
        }

        /// <summary>
        /// 网口连接
        /// </summary>
        private void btnWk_Click(object sender, EventArgs e)
        {
            //if (txtIp.Text.Trim() == string.Empty)
            //{
            //    Messages.ShowAlertDialog("请输入设备IP地址！", false);
            //    return;
            //}

            //if (!Common.CheckIP(txtIp.Text.Trim()))
            //{
            //    Messages.ShowAlertDialog("请输入正确的设备IP地址！", false);
            //    return;
            //}

            //MainForm mf = new MainForm();
            //if (mf.InitUdpClient(txtIp.Text.Trim()))
            //{
            //    MainForm.isSuccess = false;
            //    mf.SendDeviceCmd();
            //    if (mf.CheckReturnCmd())
            //    {
            //        mf.Show();
            //        this.Hide();
            //    }
            //    else
            //    {
            //        mf.CloseConnection(1);
            //        Messages.ShowAlertDialog("连接设备失败，请重新连接！", false);
            //    }
            //}
        }

        private void ConnectionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
            System.Environment.Exit(0);
        }
    }
}
